package cn.dnui_zjq823.dnuifood_16110100823.detail;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import java.util.List;

import cn.dnui_zjq823.dnuifood_16110100823.R;
import cn.dnui_zjq823.dnuifood_16110100823.adapter.FoodAdapter;

import cn.dnui_zjq823.dnuifood_16110100823.bean.FoodBean;

import cn.dnui_zjq823.dnuifood_16110100823.bean.ResultBean;
import cn.dnui_zjq823.dnuifood_16110100823.listener.onRetrofitListener;
import cn.dnui_zjq823.dnuifood_16110100823.model.CollectModel;
import cn.dnui_zjq823.dnuifood_16110100823.model.ShopModel;

/**
 * Created by dell on 2018/11/1.
 */

public class ShopDetail extends AppCompatActivity implements onRetrofitListener{
    private RecyclerView recycler_food;
    private RecyclerView.LayoutManager layoutManager;
    private FoodAdapter foodAdapter;
    private List<FoodBean> list;
    private int shop_id;
    private ImageView sd_pic;
    private TextView sd_name,sd_address,sd_phone,sd_intro,sd_level;
    private String shopname,shopaddress,shopphone,shopintro,shoppic,shoplevel;
    private String url="http://172.24.10.175:8080/foodService/";
    private Context context;
    SharedPreferences sp;
    private int userid;
    private MenuItem item;
    private CollectModel collectmodel;
    private String collected,success;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.detail_shop);
        initRecycleview();
        context=getApplicationContext();
        shopdate();
        shop_id=Integer.parseInt(getIntent().getStringExtra("shopid"));
        initJudgeCollect();
        ShopModel model=new ShopModel();
        model.getFoodByShop(shop_id,this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menucollect,menu);
        item=menu.findItem(R.id.menucollect);
        collectmodel=new CollectModel();
        sp=getSharedPreferences("login",MODE_PRIVATE);
        String id=sp.getString("userid",null);
        userid=Integer.parseInt(id);
        collectmodel.isCollected(userid,shop_id,0,this);
        return true;
    }

    private void initJudgeCollect() {
//        item=findViewById(R.id.menucollect);
//        collectmodel=new CollectModel();
//        sp=getSharedPreferences("login",MODE_PRIVATE);
//        String id=sp.getString("userid",null);
//        userid=Integer.parseInt(id);
//        collectmodel.isCollected(userid,shop_id,0,this);
    }

    private void shopdate(){
        shopname=getIntent().getStringExtra("shopname");
        shopaddress=getIntent().getStringExtra("shopaddress");
        shopphone=getIntent().getStringExtra("shopphone");
        shopintro=getIntent().getStringExtra("shopintro");
        shoppic=getIntent().getStringExtra("shoppic");
        shoplevel=getIntent().getStringExtra("shoplevel");
        sd_name=findViewById(R.id.sd_name);
        sd_address=findViewById(R.id.sd_address);
        sd_phone=findViewById(R.id.sd_phone);
        sd_intro=findViewById(R.id.sd_intro);
        sd_level=findViewById(R.id.sd_level);
        sd_pic=findViewById(R.id.sd_pic);
        Picasso.with(context).load(url+shoppic).fit().placeholder(R.mipmap.ic_launcher).into(sd_pic);
        sd_name.setText("店名："+shopname);
        sd_address.setText("地址："+shopaddress);
        sd_phone.setText("电话："+shopphone);
        sd_intro.setText("介绍："+shopintro);
        sd_level.setText("评价（1~5）："+shoplevel);
    }
    @Override
    public void onSuccess(Object object, int flag) {
        if(flag==2) {
            list = (List) object;
            foodAdapter = new FoodAdapter(this);
            foodAdapter.setList(list);
            recycler_food.setAdapter(foodAdapter);
        }
        if(flag==0){
            ResultBean resultBean;
            resultBean=(ResultBean)object;
            collected=resultBean.getCollected();
            if (collected.equals("1")){
                item.setTitle("取消收藏");
            }
            if (collected.equals("0")){
                item.setTitle("收藏");
            }
        }
        if(flag==4){
            ResultBean resultBean;
            resultBean=(ResultBean)object;
            success=resultBean.getSuccess();
            if (success.equals("1")){
                collectmodel.isCollected(userid,shop_id,0,this);
            }
        }
    }
    @Override
    public void onFalure(String msg) {
        Toast.makeText(this, "失败："+msg, Toast.LENGTH_SHORT).show();
    }

    private void initRecycleview(){
        recycler_food=findViewById(R.id.recycler_food);
        layoutManager=new LinearLayoutManager(this);
        recycler_food.setLayoutManager(layoutManager);
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.menucollect:
                    collectmodel.userCollectShop(userid,shop_id,this);
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}
